/**
 * member 账单组成员
 */
Jet().$package('app.member', function(J){
	var $=J.dom.id,
		$D=J.dom,
		$E=J.event,
		$H=J.http,
		$U=J.ui,
		$tool=J.tool;
	
	//成员列表
	this.listMember = function(){
		$D.html($('ul_member'), '<li><span class="loading loading-left">加载中...</span></li>');
		var context = this;
		this.aMemberSinaID = {};
		var uri = '/ajax.php?d=group&c=GroupProxy&f=members&group_id='+$('sel_group').value;
		$tool.get(uri,function(response){
			if(response.result==0){
				$tool.alertMsg(response.data);
				$D.empty($('ul_member'));
			}
			var data = response.data,
				creator = data.creator,
				is_creator = data.is_creator,
				sHtml = '',
				aMemberSinaID = context.aMemberSinaID;
			J.each(data.list,function(key,row){
				var image_url = row.u_image_url=='' ? '/frontend/image/who.gif' : row.u_image_url;
				sHtml += '<li id="li_member'+row.u_id+'">';
				var sImg = '<img width="50" height="50" alt="'+row.u_user_name+'" src="'+image_url+'" />';
				if(row.sina_id==0) sHtml += '<span>'+sImg+'</span>';
				else sHtml += '<span><a href="http://www.weibo.com/'+row.sina_id+'" target="_blank">'+sImg+'</a></span>';
				sHtml += '<span class="name" title="'+row.u_user_name+'">'+row.u_user_name+'</span>	';
				if(row.u_id == creator) sHtml += '<span class="link">创建者</span>';
				else if(is_creator == 1){
					var sOnclick = 'app.member.removeMember('+row.u_id+','+row.sina_id+',this);';
					sHtml+='<span class="link"><a href="javascript:void(0);" onclick="'+sOnclick+'">移除</a></span>';
				}
				sHtml += '</li>';
				aMemberSinaID['u'+row.sina_id] = 1;
			});
			$D.html($('ul_member'), sHtml);
			if(is_creator == 1){
				$D.html($('add_button'),'<a href="javascript:app.member.openSelector();" class="button-blue"><span>添加成员</span></a>');
			}
		});		
	};
	
	//打开选择器
	this.openSelector = function(){
		var _self = this;
		var oDialog = J.ui.dialog({
			'divNode':$('member_selector'), 
			'title':'添加账单组成员', 
			'width':'550px',
			'height':'350px',
			'isDrag':false,
			'isLayer':true
		});
		if(this.memberTab==undefined){
			this.memberTab = new $U.Tab();
			this.memberTab.setConfig({'defaultIndex':0});
			this.memberTab.add({trigger:$('tab_bilateral'), sheet:$('bilateral')});
			//this.memberTab.add({trigger:$('tab_search_user'), sheet:$('search_user')});
			//this.memberTab.add({trigger:$('tab_temp_user'), sheet:$('temp_user')});
			$E.addObserver(this.memberTab, "show", function(a){
				switch(this.indexOf(a)){
					case 0:
						_self.listBilateral(1);
						break;
					/*case 1:
						break;
					case 2:
						break;*/
				}
			});
			this.memberTab.init();
		}				
	};
	
	//互相关注列表
	this.listBilateral = function(page){
		$D.html($('bilateral_data'), '<li><span class="loading loading-left">加载中...</span></li>');
		var uri = '/ajax.php?d=user&c=UserProxy&f=bilateral&limit=10&page='+page;
		$tool.get(uri,function(response){
			var data = response.data;
			if(data.users==undefined) {
				$tool.alertMsg('请求数据失败');
				$D.empty($('bilateral_data'));
				return;
			}
			var sHtml = '';
			var aMemberSinaID = app.member.aMemberSinaID;
			J.each(data.users,function(key,row){
				sHtml += '<li>';
				var sImg = '<img width="50" height="50" alt="'+row.screen_name+'" src="'+row.profile_image_url+'" />';
				sHtml += '<span><a href="http://www.weibo.com/'+row.id+'" target="_blank">'+sImg+'</a></span>';
				sHtml += '<span class="name">'+row.screen_name+'</span>';
				var sOnclick = "app.member.addMember("+row.id+",'"+row.screen_name+"','"+row.profile_image_url+"',this);";
				var sLink = '<a href="javascript:void(0);" onclick="'+sOnclick+'">添加</a>';
				sHtml += '<span class="link">'+((aMemberSinaID['u'+row.id]==undefined) ? sLink : '已添加')+'</span>';
				sHtml += '</li>';
			});
			$D.html($('bilateral_data'), sHtml);			
			$U.pager({'pageNode': $('page_bilateral'), 
				'pageCurrent': page, 
				'pageCount': 10, 
				'count': data.total_number,
				'callback': app.member.listBilateral 
			});
		});
	};
			
	//添加成员	
	this.addMember = function(uid,name,img_url,aNode){
		var context = this,
			spanNode = aNode.parentNode;
		var ahtml = spanNode.innerHTML;
		$D.html(spanNode, '<div class="loading"></div>');
		var uri='/ajax.php?d=group&c=GroupProxy&f=addSinaUser';
		$tool.post(uri,{sina_id:uid,group_id:$('sel_group').value},function(response){
			if(response.result==0) {
				$tool.alertMsg(response.data);
				$D.html(spanNode, ahtml);
				return;
			}
			$D.text(spanNode, '已添加');
			var u_id = response.data.user_id;
			var liStr = '<li id="li_member'+u_id+'">';
			liStr += '<span><a href="http://www.weibo.com/'+uid+'" target="_blank">';
			liStr += '<img width="50" height="50" alt="'+name+'" src="'+img_url+'" /></a></span>';
			liStr += '<span class="name" title="'+name+'">'+name+'</span>';
			var sOnclick = 'app.member.removeMember('+u_id+','+uid+',this);';
			liStr += '<span class="link"><a href="javascript:void(0);" onclick="'+sOnclick+'">移除</a></span>';
			liStr += '</li>';			
			$D.append($('ul_member'), liStr);
			context.aMemberSinaID['u'+uid] = 1;
			app.memberKV[u_id] = name;
		});
	};
	
	//移除成员
	this.removeMember = function(user_id,sina_id,aNode){
		var context = this;
		J.tool.confirm('您确定要移除吗？', function(){
			var spanNode = aNode.parentNode;
			var ahtml = spanNode.innerHTML;
			$D.html(spanNode, '<div class="loading"></div>');
			var uri = '/ajax.php?d=group&c=GroupProxy&f=removeMember';
			$tool.post(uri,{group_id:$('sel_group').value,user_id:user_id},function(response){
				if(response.result==0) {
					$tool.alertMsg(response.data);
					$D.html(spanNode, ahtml);
				} else {
					$D.remove($('li_member'+user_id));
					$tool.showTip('移除成功');
					delete context.aMemberSinaID['u'+sina_id];	
					context.listBilateral(1);
					delete app.memberKV[user_id];
				}
			});
		});		
	};
	
	/*//我关注的人列表
	this.listFriends = function(page){
		var uri = '/ajax.php?d=user&c=UserProxy&f=friends&limit=10&page='+page;
		$tool.get(uri,function(response){
			var data = response.data;
			var sHtml = '';
			J.each(data.users,function(key,row){
				sHtml += '<li>';
				sHtml += '<span><img width="50" height="50" alt="'+row.screen_name+'" src="'+row.profile_image_url+'" /></span>';
				sHtml += '<span class="name">'+row.screen_name+'</span>	';
				sHtml += '<span class="link"><a href="javascript:app.member.addMember('+row.id+');">添加</a></span>';
				sHtml += '</li>';
			});
			$('friends_data').innerHTML = sHtml;
			new $U.Pager({'pageNode': $('page_friends'), 
				'pageCurrent': page, 
				'pageCount': 10, 
				'count': data.total_number,
				'callback': app.member.listFriends 
			});	
		});
	};
	
	//我的粉丝列表
	this.listFollowers = function(page){
		var uri = '/ajax.php?d=user&c=UserProxy&f=followers&limit=20&page='+page;
		$tool.get(uri,function(response){
			var data = response.data;
			var sHtml = '';
			J.each(data.users,function(key,row){
				sHtml += '<li>';
				sHtml += '<span><img width="50" height="50" alt="'+row.screen_name+'" src="'+row.profile_image_url+'" /></span>';
				sHtml += '<span class="name">'+row.screen_name+'</span>	';
				sHtml += '<span class="link"><a href="javascript:app.member.addMember('+row.id+');">添加</a></span>';
				sHtml += '</li>';
			});
			$('followers_data').innerHTML = sHtml;
			new $U.Pager({'pageNode': $('page_followers'), 
				'pageCurrent': page, 
				'pageCount': 20, 
				'count': data.total_number,
				'callback': app.member.listFollowers 
			});	
		});
	};*/
	
});